<!DOCTYPE html>
<html lang="${LANG}">

<head>
    <meta charset="UTF-8">
    <title>QBT_TR(Renaming)QBT_TR[CONTEXT=TorrentContentTreeView]</title>
    <link rel="stylesheet" href="css/style.css?v=${CACHEID}" type="text/css">
    <script src="scripts/lib/MooTools-Core-1.6.0-compat-compressed.js"></script>
    <script src="scripts/lib/MooTools-More-1.6.0-compat-compressed.js"></script>
    <script src="scripts/misc.js?locale=${LANG}&v=${CACHEID}"></script>
    <script src="scripts/filesystem.js?v=${CACHEID}"></script>
    <script>
        "use strict";

        new Keyboard({
            defaultEventType: "keydown",
            events: {
                "Enter": function(event) {
                    $("renameButton").click();
                    event.preventDefault();
                },
                "Escape": function(event) {
                    window.parent.qBittorrent.Client.closeWindows();
                    event.preventDefault();
                },
                "Esc": function(event) {
                    window.parent.qBittorrent.Client.closeWindows();
                    event.preventDefault();
                }
            }
        }).activate();

        window.addEventListener("DOMContentLoaded", () => {
            const hash = new URI().getData("hash");
            const oldPath = new URI().getData("path");
            const isFolder = ((new URI().getData("isFolder")) === "true");

            const oldName = window.qBittorrent.Filesystem.fileName(oldPath);
            $("rename").value = oldName;
            $("rename").focus();
            if (!isFolder)
                $("rename").setSelectionRange(0, oldName.lastIndexOf("."));

            $("renameButton").addEventListener("click", (e) => {
                e.preventDefault();
                e.stopPropagation();

                // check field
                const newName = $("rename").value.trim();
                if (newName === "") {
                    alert("QBT_TR(Name cannot be empty)QBT_TR[CONTEXT=HttpServer]");
                    return;
                }

                if (newName === oldName) {
                    alert("QBT_TR(Name is unchanged)QBT_TR[CONTEXT=HttpServer]");
                    return;
                }

                $("renameButton").disabled = true;

                const parentPath = window.qBittorrent.Filesystem.folderName(oldPath);
                const newPath = parentPath
                    ? parentPath + window.qBittorrent.Filesystem.PathSeparator + newName
                    : newName;
                new Request({
                    url: isFolder ? "api/v2/torrents/renameFolder" : "api/v2/torrents/renameFile",
                    method: "post",
                    data: {
                        hash: hash,
                        oldPath: oldPath,
                        newPath: newPath
                    },
                    onSuccess: function() {
                        window.parent.qBittorrent.Client.closeWindows();
                    },
                    onFailure: function() {
                        alert("QBT_TR(Failed to update name)QBT_TR[CONTEXT=HttpServer]");
                        $("renameButton").disabled = false;
                    }
                }).send();
            });
        });
    </script>
</head>

<body>
    <div style="padding: 10px 10px 0px 10px;">
        <label for="rename" style="font-weight: bold;">QBT_TR(New name:)QBT_TR[CONTEXT=TorrentContentTreeView]</label>
        <input type="text" id="rename" style="width: 99%;">
        <div style="text-align: center; padding-top: 10px;">
            <input type="button" value="QBT_TR(Save)QBT_TR[CONTEXT=HttpServer]" id="renameButton">
        </div>
    </div>
</body>

</html>
